package org.come.Jpanel;

import java.awt.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

import com.tool.tcpimg.UIUtils;
import com.tool.time.Limit;
import com.tool.time.TimeLimit;
import com.tool.time.TimerUtil;
import com.updateNew.MyIsif;

import org.come.Frame.TaobaoCourtMainJframe;
import org.come.bean.LoginResult;
import org.come.bean.PayvipBean;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.ScrollUI;
import org.come.until.SrcollPanelUI;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.tool.role.RoleData;

/**
 * HGC-2019-11-21 VIP面板主面板
 * 
 * @author Administrator
 * 
 */
public class VIPShopJpanel extends JPanel {
	/** 剩余天数 */
	private JLabel dayNumS;
	/**
	 * 充值反馈礼包
	 */
	private List<VipGoodsJpanel> goodsList;

	/**
	 * 滚动条
	 */
	private JScrollPane scrollPane;
	/**
	 * VIP等级
	 */
	int vipLvl = 0;

	private List<PayvipBean> payvipBeanList;

	public VIPShopJpanel() {
		this.setPreferredSize(new Dimension(656, 445));
		this.setOpaque(false);
		this.setLayout(null);
		getdayNumS();
		getScrollPane();
		getGoodsList();

	}


	/**
	 * 展示剩余时间
	 */
	public void changeTimeS() {
		Limit limit = TimeLimit.getLimits().getLimit("VIP");
		if (limit != null) {
			int residueDay = TimerUtil.residueDay(limit.getTime());
			if (residueDay >= 0) {
				dayNumS.setText("剩余"+ residueDay + "天");
				return;
			}
		}
		dayNumS.setText("--");
	}

	public JLabel getdayNumS() {
		if (dayNumS == null) {
				dayNumS = new JLabel("", SwingConstants.CENTER);
				dayNumS.setFont(UIUtils.TEXT_FONT82);
				dayNumS.setForeground(Color.black);
				dayNumS.setBounds(500, 347 + 29, 200, 16);
				dayNumS.setOpaque(false);
				this.add(dayNumS);
		}
		return dayNumS;
	}
	/**
	 * 展示滚动条物品
	 */
	public void addGoods() {
		JPanel view = (JPanel) scrollPane.getViewport().getView();
		view.removeAll();
		view.repaint();
		int num = 0;
		for (int i = 0; i < goodsList.size(); i++) {
			VipGoodsJpanel vipGoodsJpanel = goodsList.get(i);
			if (vipGoodsJpanel.getPayvipBean() == null) {
				continue;
			}
			vipGoodsJpanel.setBounds(0 + num % 4 * 140, num / 4 * 190, 136, 190);
			num++;
			view.add(vipGoodsJpanel);
		}
		view.setPreferredSize(new Dimension(565, 190 * (num % 4 == 0 ? num / 4 : num / 4 + 1)));
		scrollPane.updateUI();
		scrollPane.invalidate();
		scrollPane.validate();
		scrollPane.repaint();

		for (int i = 0; i < getIconVip().size(); i++) {
			if (vipLvl >= (i + 1)) {
				iconVip.get(i).setImage(CutButtonImage.getImage("img/item/S" + (i + 20) + ".png", 30, 25).getImage());
			} else {
				iconVip.get(i).setImage(CutButtonImage.getImage("img/item/S" + (i + 30) + ".png", 30, 25).getImage());
			}
		}
		TaobaoCourtMainJframe.getTaobaoCourtJframe().getTaobaoCourtMainJpanel().getTaobaoCourtCardJpanel().getCardLayout()
				.show(TaobaoCourtMainJframe.getTaobaoCourtJframe().getTaobaoCourtMainJpanel().getTaobaoCourtCardJpanel(), "vipshop");
	}

	/**
	 * 获取数据
	 */
	public void getShop() {
		String sendmes = Agreement.getAgreement().GetvipgradepackAgreement();
		SendMessageUntil.toServer(sendmes);
	}

	/**
	 * 获取VIP礼包列表
	 * 
	 * @param payvipBeanList
	 */
	public void showGoodsList(List<PayvipBean> payvipBeanList) {
			this.payvipBeanList = payvipBeanList;
			LoginResult loginResul = RoleData.getRoleData().getLoginResult();
			BigDecimal paysum = loginResul.getPaysum();
			int maxLevel = 0;
			if (payvipBeanList.size() == 0) {
				for (int i = 0; i < goodsList.size(); i++) {
					goodsList.get(i).showGoods(null);
				}
			} else {
				String vipget = RoleData.getRoleData().getLoginResult().getVipget();
				String[] split = null;
				if(vipget!=null) {
					split = vipget.split("&&");
				}
				for (int i = 0; i < goodsList.size(); i++) {
					if (i > payvipBeanList.size()) {
						goodsList.get(i).showGoods(null);
					} else if (i < payvipBeanList.size()) {
						PayvipBean payvipBean = payvipBeanList.get(i);
						if (goodsList.size() < payvipBeanList.size()) {
							goodsList.add(new VipGoodsJpanel());
						}
						if (paysum.compareTo(new BigDecimal(payvipBeanList.get(i).getPaynum())) >= 0) {
							vipLvl = vipLvl > payvipBean.getGrade() ? vipLvl : payvipBean.getGrade();
						}
						maxLevel = maxLevel > payvipBean.getGrade() ? maxLevel : payvipBean.getGrade();
						goodsList.get(i).showGoods(payvipBean);
						if (ImpactGradeGoodsJpanel.checkYesOrNo(split, "1", payvipBean.getGrade() + "")) {
							goodsList.get(i).getVipShopBtn().setBtn(-1);
							goodsList.get(i).getVipShopBtn().setIcon(CutButtonImage.getImage("inkImg/old/background/gift-received-btn.png", -1, -1));
						} else {
							goodsList.get(i).getVipShopBtn().setBtn(1);
							try {
								goodsList.get(i).getVipShopBtn().setIcons(CutButtonImage.cuts("inkImg/old/background/gift-receive-btn.png"));
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
				langNum = getLangNum(maxLevel, paysum);
			}
			addGoods();
	}
	/**
	 * 刷新面板
	 */
	public void refreshPanel(){
		showGoodsList(payvipBeanList);
	}

	public int getLangNum(Integer gradeInteger, BigDecimal paysum) {
		BigDecimal divide = getVipLevel(paysum).divide(new BigDecimal(gradeInteger), 3, BigDecimal.ROUND_HALF_UP);
		BigDecimal multiply = divide.multiply(new BigDecimal(587)).setScale(0, BigDecimal.ROUND_DOWN);
		return langNum = (multiply.intValue() > 587 ? 587 : multiply.intValue());
	}

	public BigDecimal getVipLevel(BigDecimal paysum) {
		BigDecimal level = new BigDecimal(0);
		if (paysum.compareTo(new BigDecimal(30)) < 0) {
			level = new BigDecimal(0);
		} else if (paysum.compareTo(new BigDecimal(88)) < 0) {
			level = new BigDecimal(1);
		} else if (paysum.compareTo(new BigDecimal(288)) < 0) {
			level = new BigDecimal(2);
		} else if (paysum.compareTo(new BigDecimal(588)) < 0) {
			level = new BigDecimal(3);
		} else if (paysum.compareTo(new BigDecimal(888)) < 0) {
			level = new BigDecimal(4);
		} else if (paysum.compareTo(new BigDecimal(1888)) < 0) {
			level = new BigDecimal(5);
		} else if (paysum.compareTo(new BigDecimal(2888)) < 0) {
			level = new BigDecimal(6);
		} else if (paysum.compareTo(new BigDecimal(3888)) < 0) {
			level = new BigDecimal(7);
		} else if (paysum.compareTo(new BigDecimal(4888)) < 0) {
			level = new BigDecimal(8);
		} else if (paysum.compareTo(new BigDecimal(5888)) < 0) {
			level = new BigDecimal(9);
		}
		return level;
	}

	/** 当前VIP进度 */
	private int langNum = 0;

	private ImageIcon iconTop;

	private ImageIcon iconBack;

	private ImageIcon iconPlan;

	private List<ImageIcon> iconVip;

	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
			if (iconTop == null) {
				iconTop = CutButtonImage.getImage("inkImg/old/background/S4.png", -1, -1);
			}
			g.drawImage(iconTop.getImage(), 160, 5, 591, 116, this);
			if (iconBack == null) {
				iconBack = CutButtonImage.getImage("inkImg/old/background/S5.png", -1, -1);
			}
			g.drawImage(iconBack.getImage(), 160, 135, 587, 7, this);
			if (iconPlan == null) {
				iconPlan = CutButtonImage.getImage("inkImg/old/background/S6.png", -1, -1);
			}
			g.drawImage(iconPlan.getImage(), 160, 136, langNum, 5, this);

			for (int i = 0; i < getIconVip().size(); i++) {
				g.drawImage(getIconVip().get(i).getImage(), 168 + 63 * i, 127, 30, 25, this);
			}
			g.setFont(UIUtils.TEXT_FONT2);
			g.setColor(Color.black);
			g.drawString("累计充值积分:", 250, 480);
			g.setColor(Color.red);
			g.drawString(RoleData.getRoleData().getLoginResult().getMoney().toString()+" 积分",335, 480);
			g.setColor(Color.black);
			g.drawString("累计充值金额:", 400, 480);
			g.setColor(Color.red);
			g.drawString(RoleData.getRoleData().getLoginResult().getPaysum().toString()+" 元",485, 480);
	}

	public JScrollPane getScrollPane() {
			if (scrollPane == null) {
				// 属性滚动条
				scrollPane = new JScrollPane();
				scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
				scrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
				scrollPane.getVerticalScrollBar().setUnitIncrement(190);
				scrollPane.getViewport().setOpaque(false);
				scrollPane.setOpaque(false);
				scrollPane.setBounds(165, 155, 591, 290);
				scrollPane.setBorder(BorderFactory.createEmptyBorder());
				scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
				JPanel jPanel = new JPanel();
				jPanel.setBackground(null);
				jPanel.setOpaque(false);
				jPanel.setBorder(null);
				jPanel.setLayout(null);
				scrollPane.setViewportView(jPanel);
				this.add(scrollPane);
			}
		return scrollPane;
	}

	public void setScrollPane(JScrollPane scrollPane) {
		this.scrollPane = scrollPane;
	}

	public ImageIcon getIconTop() {
		return iconTop;
	}

	public void setIconTop(ImageIcon iconTop) {
		this.iconTop = iconTop;
	}

	public List<VipGoodsJpanel> getGoodsList() {
		if (goodsList == null) {
			goodsList = new ArrayList<>();
			for (int i = 0; i < 15; i++) {
				VipGoodsJpanel vipGoodsJpanel = new VipGoodsJpanel();
				goodsList.add(vipGoodsJpanel);
			}
		}
		return goodsList;
	}

	public void setGoodsList(List<VipGoodsJpanel> goodsList) {
		this.goodsList = goodsList;
	}

	public ImageIcon getIconBack() {
		return iconBack;
	}

	public void setIconBack(ImageIcon iconBack) {
		this.iconBack = iconBack;
	}

	public ImageIcon getIconPlan() {
		return iconPlan;
	}

	public void setIconPlan(ImageIcon iconPlan) {
		this.iconPlan = iconPlan;
	}

	public List<ImageIcon> getIconVip() {
		if (iconVip == null) {
			iconVip = new ArrayList<>();
			for (int i = 0; i < 10; i++) {
				iconVip.add(CutButtonImage.getImage("img/item/S" + (i + 30) + ".png", 30, 25));
			}
		}
		return iconVip;
	}

	public void setIconVip(List<ImageIcon> iconVip) {
		this.iconVip = iconVip;
	}

}
